/*
 * Sycloud.com Inc.
 * Copyright (c) 2005-2066 All Rights Reserved.
 */
package com.insigma.business.qrcode.welfare._provider.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.common.utils.CurrentUserUtil;
import com.insigma.business.common.utils.ToolsHelper;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.placement.dto.PlacementPubInfoDTO;
import com.insigma.business.qrcode.placement.entity.PlacementPubInfoEntity;
import com.insigma.business.qrcode.soldier.dto.GatewayTypeEnum;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.welfare.dto.CareWelfareServiceOrganizationDTO;
import com.insigma.business.qrcode.welfare.entity.CareWelfareServiceOrganizationDO;
import com.insigma.business.qrcode.welfare.request.CareWelfareServiceOrganizationInsertRequest;
import com.insigma.business.qrcode.welfare.request.CareWelfareServiceOrganizationSearchRequest;
import com.insigma.business.qrcode.welfare.dto.CareWelfareServiceOrganizationBSearcher;
import com.insigma.business.qrcode.common.converter.CareWelfareServiceOrganizationProviderConverter;
import com.insigma.business.qrcode.welfare.request.CareWelfareServiceOrganizationUpdateRequest;
import com.insigma.business.qrcode.welfare.service.CareWelfareServiceOrganizationService;
import com.insigma.business.qrcode.welfare._provider.CareWelfareServiceOrganizationProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 福利-服务机构服务提供实现类
 * @author juffett
 * @version $Id: CareWelfareServiceOrganizationProviderImpl.java, v 1.0.0 2021-9-9 14:45:13 juffett Exp $
 */
@Slf4j
@Service
@Component("careWelfareServiceOrganizationProvider")
public class CareWelfareServiceOrganizationProviderImpl extends BaseProviderImpl implements CareWelfareServiceOrganizationProvider {

    @Autowired
    private CareWelfareServiceOrganizationService careWelfareServiceOrganizationService;

    /**
     * @see CareWelfareServiceOrganizationProvider#searchCareWelfareServiceOrganization(CareWelfareServiceOrganizationSearchRequest)
     */
    @Override
    public BasePagingResponse<List<CareWelfareServiceOrganizationDTO>> searchCareWelfareServiceOrganization(final CareWelfareServiceOrganizationSearchRequest searchRequest) {
        final BasePagingResponse<List<CareWelfareServiceOrganizationDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareServiceOrganization的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CareWelfareServiceOrganizationBSearcher careWelfareServiceOrganizationBSearcher = CareWelfareServiceOrganizationProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
                final List<CareWelfareServiceOrganizationDO> careWelfareServiceOrganizationDOList = careWelfareServiceOrganizationService.search(careWelfareServiceOrganizationBSearcher);
                PageInfo<CareWelfareServiceOrganizationDO> pageInfo = new PageInfo<>(careWelfareServiceOrganizationDOList);
                List<CareWelfareServiceOrganizationDTO> careWelfareServiceOrganizationDTOS = ToolsHelper.copyListProperties(pageInfo.getList(), CareWelfareServiceOrganizationDTO::new);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareServiceOrganizationDTOS, searchRequest, pageInfo.getTotal());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_SEARCH;
            }
        });

        return basePagingResponse;
    }


    /**
    * @see CareWelfareServiceOrganizationProvider#getCareWelfareServiceOrganization(BaseGetRequest)
    */
    @Override
    public BaseResponse<CareWelfareServiceOrganizationDTO> getCareWelfareServiceOrganization(final BaseGetRequest baseGetRequest){
        final BaseResponse<CareWelfareServiceOrganizationDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CareWelfareServiceOrganizationDO careWelfareServiceOrganizationDO = careWelfareServiceOrganizationService.get((String)baseGetRequest.getId());
                final CareWelfareServiceOrganizationDTO remoteObj = CareWelfareServiceOrganizationProviderConverter.convertResponseDO2DTO(careWelfareServiceOrganizationDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> insertCareWelfareServiceOrganization(CareWelfareServiceOrganizationInsertRequest insertRequest) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CareWelfareServiceOrganization的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final CareWelfareServiceOrganizationDO careWelfareServiceOrganizationDO = CareWelfareServiceOrganizationProviderConverter.convertRequestIO2DO(insertRequest);
                careWelfareServiceOrganizationDO.setCreatorId(CurrentUserUtil.getCurrentUser().getUserId());
                careWelfareServiceOrganizationDO.setCreatorType("GOV_EMPLOYEE");
                careWelfareServiceOrganizationDO.setModifierId(CurrentUserUtil.getCurrentUser().getUserId());
                careWelfareServiceOrganizationDO.setModifierType("GOV_EMPLOYEE");
                careWelfareServiceOrganizationService.insert(careWelfareServiceOrganizationDO);
                ResponseUtils.setupSuccessResponse(baseResponse, careWelfareServiceOrganizationDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> updateCareWelfareServiceOrganization(CareWelfareServiceOrganizationUpdateRequest updateRequest) {
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                CareWelfareServiceOrganizationDO careWelfareServiceOrganizationDO = CareWelfareServiceOrganizationProviderConverter.convertRequestUO2DO(updateRequest);
                careWelfareServiceOrganizationDO.setModifierId(CurrentUserUtil.getCurrentUser().getUserId());
                careWelfareServiceOrganizationDO.setModifierType("GOV_EMPLOYEE");
                Integer affectRowNum = careWelfareServiceOrganizationService.update(careWelfareServiceOrganizationDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> deleteCareWelfareServiceOrganization(BaseDeleteRequest deleteRequest) {
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CareWelfareServiceOrganization的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = careWelfareServiceOrganizationService.delete((String)deleteRequest.getId(),"GOV_EMPLOYEE", CurrentUserUtil.getCurrentUser().getUserId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_SERVICE_ORGANIZATION_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> publishOrg(CareWelfareServiceOrganizationUpdateRequest request) {
        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getId(), "id不能为空");
        BizCheckUtil.checkNotNull(request.getPublished(), "状态不能为空");

        CareWelfareServiceOrganizationDO careWelfareServiceOrganizationDO = careWelfareServiceOrganizationService.get(request.getId());
        BizCheckUtil.checkNotNull(careWelfareServiceOrganizationDO, "不存在");
        careWelfareServiceOrganizationDO.setPublished(request.getPublished());
        Integer update = careWelfareServiceOrganizationService.update(careWelfareServiceOrganizationDO);
        return ResponseUtils.buildSuccessResponse(update);
    }
}
